package model.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import model.bean.Evento;
import model.bean.Participante;
import model.factory.HibernateUtil;
import model.interfaces.IParticipante;

public class ParticipanteDAO implements IParticipante {
	
	private Session session;
	
	@Override //correto
	public void cadastrarParticipante(Participante participante) {
		try {
			session = HibernateUtil.getSession();
			Transaction tx = session.beginTransaction();
			session.saveOrUpdate(participante);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override //correto
	public Participante findParticipanteById(int idParticipante) {
		session = HibernateUtil.getSession();
		Query q = session.createQuery("FROM Participante p WHERE p.participanteId = :idParticipante");
		q.setInteger("idParticipante", idParticipante);
		return (Participante) q.uniqueResult(); 
	}

	@Override //correto
	public void removerParticipantePeloNome(String nomeParticipante) {
		session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		String hql = " DELETE FROM Participante p WHERE p.nome LIKE :nomeParticipante ";
		Query q = session.createQuery(hql);
		q.setString("nomeParticipante", nomeParticipante);
		q.executeUpdate();
		tx.commit();
	}

	@Override
	public void removerParticipantePeloId(int idParticipante) {
		session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		String hql = " DELETE FROM Participante p WHERE p.participanteId = :idParticipante ";
		Query q = session.createQuery(hql);
		q.setInteger("idParticipante", idParticipante);
		q.executeUpdate();
		tx.commit();
		
	}

	@Override
	public void vinculaEventoParticipantePeloNome(String nomeEvento,
			String nomeParticipante) {
		session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		String sql = " INSERT INTO evento_participante VALUES ( "
				+ " (SELECT DISTINCT e.id_evento FROM evento e WHERE e.nome LIKE :nomeEvento) , "
				+ " (SELECT DISTINCT p.id_participante FROM participante p WHERE p.nome LIKE :nomeParticipante ) "
				+ " )";
		Query q = session.createSQLQuery(sql);
		q.setString("nomeEvento", nomeEvento);
		q.setString("nomeParticipante", nomeParticipante);
		q.executeUpdate();
		tx.commit();

		
	}

	@Override
	public void solitarPresenca() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void solicitarControle() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean statusPresenca() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean getPresenca() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String getMatricula() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getNome() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getLogin() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	
}
